<?php

namespace Parser;

use Parser\Separator\baseSeparator;

class Quote
{
    public $rawData;

    public $code = '200';

    public $message;

    /**
     * @var BaseSeparator
     */
    public $separator;

    public $insureSource;

    public $predict;

    public $quoteNo;

    public $orderId;

    public $engineNo;

    public $licenseNo;

    public $vin;

    public $companyId;

    public $batch;

    public $result;

    public $independentPriceRatio;

    private $type;

    public function __construct(array $predict,$type = 'quote')
    {
        $this->type = $type;
        $this->code = $predict['code'] ?? '500';
        $this->message = $predict['message'] ?? '错误数据';

        if ($this->type != 'batch') {
            $this->predict = $predict['data'][0];
        } else {
            $this->predict = $predict;
            $this->batch = $this->predict['batch'] ?? '';
        }
        $this->insureSource = $this->predict['insureSystemCode'] ?? $this->predict['insureSource'] ?? 'PICC';
        $this->quoteNo = $this->predict['quoteNo'] ?? '';
        $this->independentPriceRatio = (string)($this->predict['independentPriceRatio'] ?? '');
        $this->vin = $this->predict['vinNo'] ?? null;
        $this->engineNo = $this->predict['engineNo'] ?? null;
        $this->licenseNo = $this->predict['licenseNo'] ?? null;

        $this->companyId = substr($this->quoteNo, strpos($this->quoteNo, ':') + 1);

        $this->rawData = json_decode($this->predict['rawData'], true);

        if ($this->message = $this->predict['remark'] ?? false) {
            return;
        }
        if ($this->type != 'batch') {
            if (!$this->orderId = $this->predict['orderId'] ?? false) {
                $this->message = '未找到订单信息';
                return;
            }
        }

        $this->separation();
    }

    public function result(): array
    {
        return [
            'code' => $this->code,
            'message' => $this->message,
            'data' => $this->result,
        ];
    }


    private function separation()
    {

        $className = "Parser\\Separator\\" . $this->insureSource;

        /**
         * @var Separator\baseSeparator $separator
         */
        $separator = new $className;

        $separator->interpreter($this->rawData,$this);
    }
}